﻿using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Cottages
{
    public class CottageBcakFillHander : IRequestHandler<CottageBcakFillCommand, Result>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_cottageRepository"></param>
        /// <param name="mapper"></param>
        public CottageBcakFillHander(IBaseRepository<Cottage> _cottageRepository, IMapper mapper)
        {
            this._cottageRepository = _cottageRepository;
            this._mapper = mapper;

        }

        /// <summary>
        /// 栋舍编辑查看详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(CottageBcakFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var cottage = await(await _cottageRepository.GetAll()).Where(c => c.CottageId == request.CottageId).FirstOrDefaultAsync();
                if (cottage != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = cottage;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
